#!/bin/bash
# This script takes one argument: the path to the jscoverage executable

set -e

# Make sure this is run from the root of the project.
cd `hg root`

# Create a mutable copy of the source files for the Firefox extension.
mkdir -p build/firefox/
cp -r firefox/* build/firefox

# Generate the instrumented files. These must exist before prepare_html.py is run.
./instrument.sh $1

# Include the content of jscoverage.html as a JavaScript string variable. 
python prepare_html.py
cat build/jsc_var.js firefox/chrome/content/jsc_header.js > build/jsc_header.js

# Include instrumented JSCoverage files in the extension.
# Add a fixed header (the contents of jsc_header.js) to each instrumented file.
find build/instrumented/ -name '*.js' | \
    xargs -I {} sh -c 'cat build/jsc_header.js {} > build/firefox/chrome/content/`basename {}`'
cp build/instrumented/jscoverage.html build/firefox/chrome/content/

# Zip up the extension.
# TODO(bolinfest): Is there a way to do this without pushd/popd?
pushd build/firefox
zip -r ../jsbloat.xpi .
popd
